﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace QLKS.Models
{
    static public class KhachHangModel
    {
        //Lấy danh sách khách hàng
        static public DataTable GetAllKhachHang(string selectColumns)
        {
            return DataProviderModel.QueryResults(string.Format("select {0} from v_dmKhachHang", selectColumns.Trim() == "" ? "*" : selectColumns)).Tables[0];
        }

        //Lấy tất cả khách hàng
        static public DataTable GetAllKhachHang()
        {
            DataTable tb = new DataTable();
            try
            {
                tb = DataProviderModel.QueryResults("SELECT * FROM v_dmKhachHang").Tables[0];
            }
            catch { }
            return tb;
        }

        //Lấy thông tin chi tiết của khách hàng
        static public DataTable GetKhachHang(string makh)
        {
            return DataProviderModel.QueryResults("Select * from v_dmKhachHang WHERE makh = '" + makh + "'").Tables[0];
        }
        //Tạo mới khách hàng
        static public DataTable CreateNewKhachHang()
        {
            DataTable tb = DataProviderModel.QueryResults("SELECT * FROM dmKhachHang WHERE 1=0").Tables[0];
            DataRow row = tb.NewRow();
            row["makh"] = GetMaKhMoi();
            row["nhomkh"] = GetMaNhom();
            row["gioitinh"] = "Nam";
            row["namsinh"] = DateTime.Today.Year;
            row["ghichu"] = "";
            tb.Rows.Add(row);
            return tb;
        }

        static public int GetMaNhom()
        {
            return (int)DataProviderModel.ScalarExecute("SELECT Min(manhkh) FROM NhomKhachHang");
        }
        //Lấy Nhóm khách hàng
        static public DataTable GetNhomKhachHang()
        {
            return DataProviderModel.QueryResults("select * from NhomKhachHang").Tables[0];
        }

        //Lấy Mã Khách Hàng Tiếp Theo //KH00001
        static public string GetMaKhMoi()
        {
            object MaKh = DataProviderModel.ScalarExecute("SELECT MAX(makh) FROM dmKhachHang");
            int stt = 0;
            if (MaKh.GetType() == typeof(System.DBNull))
                stt = 0;
            else
                stt = int.Parse(MaKh.ToString().Substring(2));
            stt++;
            return "KH" + DataProviderModel.LeftPad(stt.ToString(), 5 - stt.ToString().Length, "0");
        }

        //Thêm khách hàng
        static public int ThemKhachHang(DataTable tb)
        {
            //DataTable tb = DataProviderModel.CreateColumns("dmKhachHang", "");
            //tb.Rows.Add(ma, hoten, nhomkh, cmnd, gioitinh, namsinh, diachi, dienthoai, ghichu);
            string makh = tb.Rows[0]["makh"].ToString();
            if (GetAllKhachHang("makh").Select("makh ='" + makh + "'").Length <= 0)
                return DataProviderModel.Insert(tb, "dmKhachHang", "");
            else
                return DataProviderModel.Update(tb, "dmKhachHang", "makh");
        }

        //Xóa  khách hàng
        static public int XoaKhachHang(string ma)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("makh");
            tb.Rows.Add(ma);
            return DataProviderModel.Detele(tb, "dmKhachHang");
        }
    }
}
